package com.john.bluetoothtest2;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;


import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;

public class Welcome_interface extends AppCompatActivity {

    private long firstTime=0;

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        switch(keyCode){
            case KeyEvent.KEYCODE_BACK://点击返回键
                long secondTime = System.currentTimeMillis();//以毫秒为单位
                if(secondTime -firstTime>2000){
                    Toast.makeText(this, "再按一次返回退出程序", Toast.LENGTH_SHORT).show();
                    firstTime=secondTime;
                }else{
                    finish();
                    System.exit(0);
                }
                return true;
        }
        return super.onKeyUp(keyCode, event);
    }




    //实例化TextToSpeech类
    private TextToSpeech textToSpeech;
    private Context mContext;
    private static int PERMISSION_GRANTED = 0;
    private String[] permissions = new String[]{android.Manifest.permission.INTERNET, android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
            android.Manifest.permission.ACCESS_NETWORK_STATE, android.Manifest.permission.SYSTEM_ALERT_WINDOW,
            android.Manifest.permission.ACCESS_COARSE_LOCATION, android.Manifest.permission.ACCESS_FINE_LOCATION};

    private static final String TAG = "";




    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome_interface);
        all();
    }

    private void all() {
        mContext = Welcome_interface.this;

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);// 设置全屏

        TextToSpeech.OnInitListener onInitListener = new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status == TextToSpeech.SUCCESS) {
                    int result = textToSpeech.setLanguage(Locale.CHINA);
                    if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                        Toast.makeText(Welcome_interface.this, "LANG_MISSING_DATA  or LANG_NOT_SUPPORTED !", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        };
        textToSpeech = new TextToSpeech(this, onInitListener);


        startMainActivity();
    }

    private void startMainActivity() {
        textToSpeech.setPitch(1.0f); // 在系统设置里也可以修改音调
        textToSpeech.speak("欢迎使用蓝牙打卡系统", TextToSpeech.QUEUE_FLUSH, null);

        TimerTask delayTask = new TimerTask() {
            @Override
            public void run() {
                Intent mainIntent = new Intent(Welcome_interface.this, MainActivity.class);
                startActivity(mainIntent);
                finish();
            }
        };
        Timer timer = new Timer();
        timer.schedule(delayTask, 3000);//延时两秒执行 run 里面的操作
    }


}
